-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

---@type GM_Tps_C
local M = UnLua.Class()

local Screen = require "Screen"
local Const = require "Const"

function M:UserConstructionScript()
  -- self.DefaultPawnClass = UE.UClass.Load(Const.CBP_MAP_Class)
  self.DefaultPawnClass = UE.UClass.Load(Const.CBP_Class)
end

function M:ReceiveBeginPlay()
	self.EnemySpawnInterval = 2.0
	self.MaxEnemies = 3
	self.AliveEnemies = 0
	self.SpawnOrigin = UE.FVector(3000.0, 2700.0, 100.0)
	self.SpawnLocation = UE.FVector()
	self.AICharacterClassArray = {
    -- UE.UClass.Load(Const.Goblin_Class),
    -- UE.UClass.Load(Const.Ghoul_Class),
    UE.UClass.Load(Const.Zombie_Class),
  }
	-- UE.UKismetSystemLibrary.K2_SetTimerDelegate({self, M.SpawnEnemy}, self.EnemySpawnInterval, true)
end

function M:SpawnEnemy()
	local PlayerCharacter = UE.UGameplayStatics.GetPlayerCharacter(self, 0)
    -- Screen.Print(string.format("alive/max: %d/%d", self.AliveEnemies, self.MaxEnemies))
	if self.AliveEnemies < self.MaxEnemies and PlayerCharacter then
		UE.UNavigationSystemV1.K2_GetRandomReachablePointInRadius(self, self.SpawnOrigin, self.SpawnLocation, 2000)
		self.SpawnLocation.Z = self.SpawnLocation.Z + 100
		local Target = PlayerCharacter:K2_GetActorLocation()
		local SpawnRotation = UE.UKismetMathLibrary.FindLookAtRotation(self.SpawnLocation, Target)
    local randNum = math.random(#self.AICharacterClassArray)
    -- Screen.Print(string.format("rand: %d/%d", randNum, #self.AICharacterClassArray))
		UE.UAIBlueprintHelperLibrary.SpawnAIFromClass(self, self.AICharacterClassArray[randNum], nil, self.SpawnLocation, SpawnRotation)
		self.AliveEnemies = self.AliveEnemies + 1
		if self.AliveEnemies > self.MaxEnemies then
			self.AliveEnemies = self.MaxEnemies
		end
	end
end

function M:NotifyEnemyDied()
	self.AliveEnemies = self.AliveEnemies - 1
	if self.AliveEnemies < 0 then
		self.AliveEnemies = 0
	end
end

return M
